﻿
using System.Drawing;

namespace Coldairarrow.Util
{
    public class ImageHepler
    {

        public ImageHepler()
        {

        }
        /// <summary>
        /// 方法一
        /// 多张图片的合并
        /// </summary>
        static private void CombinImage()
        {
            const string folder = @"F:\测试图片";
            Image img1 = Image.FromFile(Path.Combine(folder, "测试1.png"));
            Bitmap map1 = new Bitmap(img1);
            Image img2 = Image.FromFile(Path.Combine(folder, "测试2.png"));
            Bitmap map2 = new Bitmap(img2);
            Image img3 = Image.FromFile(Path.Combine(folder, "测试3.png"));
            Bitmap map3 = new Bitmap(img3);
            var width = Math.Max(img1.Width, img2.Width);
            var height = img1.Height + img2.Height + 10;
            // 初始化画布(最终的拼图画布)并设置宽高
            Bitmap bitMap = new Bitmap(width, height);
            // 初始化画板
            Graphics g1 = Graphics.FromImage(bitMap);
            // 将画布涂为白色(底部颜色可自行设置)
            g1.FillRectangle(Brushes.White, new Rectangle(0, 0, width, height));
            //在x=0，y=0处画上图一
            g1.DrawImage(map1, 0, 0, img1.Width, img1.Height);
            //在x=0，y在图一往下10像素处画上图二
            g1.DrawImage(map2, 0, img1.Height + 10, img2.Width, img2.Height);
            g1.DrawImage(map3, 0, img1.Height + 10, img3.Width, img3.Height);
            map1.Dispose();
            map2.Dispose();
            map3.Dispose();
            Image img = bitMap;
            //保存
            img.Save(Path.Combine(folder, "new测试1.png"));
            img.Dispose();
        }

        /// <summary>
        /// 方法二
        ///实现左右拼接图片
        /// </summary>
        /// <param name="Img1"></param>
        /// <param name="Img2"></param>
        static private void CombinImage2(Image Img1, Image Img2)
        {
#if FALSE
            //控制台调用
             const string folder = @"F:\测试图片";
            Image img1 = Image.FromFile(Path.Combine(folder, "测试1.png"));
            Image img2 = Image.FromFile(Path.Combine(folder, "测试2.png"));
            JoinImage(img1, img2);
#endif
            int imgHeight = 0, imgWidth = 0;
            imgWidth = Img1.Width + Img2.Width;
            imgHeight = Math.Max(Img1.Height, Img2.Height);
            Bitmap joinedBitmap = new Bitmap(imgWidth, imgHeight);
            Graphics graph = Graphics.FromImage(joinedBitmap);
            graph.DrawImage(Img1, 0, 0, Img1.Width, Img1.Height);
            graph.DrawImage(Img2, Img1.Width, 0, Img2.Width, Img2.Height);
            Image img = joinedBitmap;
            //保存
            const string folder = @"F:\测试图片";
            img.Save(Path.Combine(folder, "new测试2.png"));
            img.Dispose();
        }
        /// <summary>
        /// 方法三
        /// 调用此函数后使此两种图片合并，类似相册，有个
        /// 背景图，中间贴自己的目标图片
        /// </summary>
        /// <param name="sourceImg">粘贴的源图片</param>
        /// <param name="destImg">粘贴的目标图片</param>
        public static void CombinImage3()
        {
            const string folder = @"D:\项目\酒商城\winescapi\src\Coldairarrow.Api\wwwroot\ExportQrCode\";
            Image img1 = Image.FromFile(Path.Combine(folder, "bgimage.png"));//相框图片 
            Image img11 = Image.FromFile(Path.Combine(folder, "1.jpg"));//相框图片 
            Image img2 = Image.FromFile(Path.Combine(folder, "qrcodeimage.Jpeg")); //照片图片               
                                                                                   //从指定的System.Drawing.Image创建新的System.Drawing.Graphics       
                                                                                   //Graphics g = Graphics.FromImage(img1);
                                                                                   // 初始化画布(最终的拼图画布)并设置宽高
                                                                                   // 初始化画布(最终的拼图画布)并设置宽高
            Bitmap bitMap = new Bitmap(900, 900);
            // 初始化画板
            Graphics g = Graphics.FromImage(bitMap);
            //Bitmap bitMap = new Bitmap(img1, 340, 340);
            Bitmap map1 = new Bitmap(img1);
            Bitmap map2 = new Bitmap(img2);
            g.DrawImage(map1, 0, 0, 340, 440);
            //Graphics g = Graphics.FromImage(img11);
            //g.DrawImage(img1, 0, 0, 340, 440);
            //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
            //g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框
            //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
            g.DrawImage(map2, 80, 150, 180, 180);
            GC.Collect();
            img1.Save(Path.Combine(folder, "new测试3.png"));
            img1.Dispose();
        }

        public static string CombineImage(string image1url,string image2url,string savepath)
        {
            string savename = DateTime.Now.ToString("yyyyMMddHHmmssffffff")+".png";
            Bitmap bmp = new Bitmap(340,440);
            Image PlayerPic1 = Image.FromFile(image1url);//需要在画图上画的图片1
            Image PlayerPic2 = Image.FromFile(image2url);//需要在画图上画的图片2
            Graphics NewPic = Graphics.FromImage(bmp);
            //画图片1的坐标以及大小
            Rectangle PlayerPicStangle1 = new Rectangle(0, 0, 340, 440);
            //绘制图片（合并2张图片）
            NewPic.DrawImage(PlayerPic1, PlayerPicStangle1);
            //画图片2的坐标以及大小
            Rectangle PlayerPicStangle2 = new Rectangle(82, 151, 180, 180);
            NewPic.DrawImage(PlayerPic2, PlayerPicStangle2);
            string url = Path.Combine(savepath, savename);
            //保存图片
            bmp.Save(url);
            string[] sArray = url.Split(new string[] { "wwwroot" }, StringSplitOptions.RemoveEmptyEntries);
            if (sArray.Length==2)
            {
                url = sArray[1].Replace("\\", "/");
            }
            else if (sArray.Length==3)
            {
                url = sArray[2].Replace("\\", "/");
            }
            return url;
        }

        /// <summary>
        /// 方法三
        /// 调用此函数后使此两种图片合并，类似相册，有个
        /// 背景图，中间贴自己的目标图片
        /// </summary>
        /// <param name="sourceImg">粘贴的源图片</param>
        /// <param name="destImg">粘贴的目标图片</param>
        static private void CombinImage4()
        {
            const string folder = @"F:\测试图片";
            Image img1 = Image.FromFile(Path.Combine(folder, "测试1.png"));//相框图片 
            Image img2 = Image.FromFile(Path.Combine(folder, "测试2.png")); //照片图片               
            //从指定的System.Drawing.Image创建新的System.Drawing.Graphics       
            Graphics g = Graphics.FromImage(img1);
            g.DrawImage(img1, 0, 0, 148, 124);// g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
                                              //g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框
                                              //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
            g.DrawImage(img2, 17, 17, 112, 73);
            GC.Collect();
            img1.Save(Path.Combine(folder, "new测试3.png"));
            img1.Dispose();
        }
    }

}
